/**
 * @Time: 2018/11/19 16:37
 * @Author: wangmin
 * @File: admin
 * @Software: GoLand
 */
package admin

import (
	"encoding/json"
	"fmt"
	"getshop/redis"
	redigo "github.com/gomodule/redigo/redis"
	"time"
)

type Admin struct {
	Aid       int
	IsLocking bool
	LoginET   int
	Token     map[int8]string
}

//get key
func (r *Admin) GetKey() string {
	return fmt.Sprintf(redis.Prefix+"admin.%d", r.Aid)
}

//读取
func (r *Admin) Read() {
	c := redis.Pool.Get()
	defer c.Close()
	b, _ := redigo.Bytes(c.Do("GET", r.GetKey()))
	if len(b) == 0 {
		return
	}
	json.Unmarshal(b, r)
}

//保存
func (r *Admin) Save() {
	c := redis.Pool.Get()
	defer c.Close()
	b, _ := json.Marshal(r)
	c.Do("SET", r.GetKey(), string(b), "EX", time.Now().Add(time.Second*3600*24).Unix())
}

//删除
func (r *Admin) Del() {
	c := redis.Pool.Get()
	defer c.Close()
	redigo.String(c.Do("DEL", r.GetKey()))
}

//NewAdmin
func NewAdmin() *Admin {
	return &Admin{
		Token: make(map[int8]string),
	}
}
